import Cookies from 'js-cookie'

const KeyJwt = 'cookie_xzdl_bms_token'
const KeyUsername = 'cookie_xzdl_bms_username'
const KeyPassword = 'cookie_xzdl_bms_password'
const KeyConfigUpload = 'cookie_xzdl_bms_config_upload'
const KeyConfigSystem = 'cookie_xzdl_bms_config_system'

// 处理cookie的通用方法

// 添加值为普通类型的cookie
function setCK(key, strVal) {
  Cookies.set(key, strVal)
}

// 添加值为对象类型的cookie
function setCKObj(key, obj) {
  return Cookies.set(key, JSON.stringify(obj))
}

// 得到值为普通类型的cookie
function getCK(key) {
  return Cookies.get(key)
}

// 得到值为对象类型的cookie值
function getCKObj(key) {
  const result = Cookies.get(key)
  if (!result) {
    return ''
  } else {
    return JSON.parse(result)
  }
}

// 删除指定key的cookie
function removeCookie(key) {
  return Cookies.remove(key)
}

// 清除所有 cookie
export function clearAllCookie() {
  removeCookieJwt()
  removeCookieUsername()
  removeCookiePassword()
  removeCookieConfigUpload()
}

// ========== 处理jwt的cookie

export function getCookieJwt() {
  return getCK(KeyJwt)
}

export function setCookieJwt(token) {
  return setCK(KeyJwt, token)
}

export function removeCookieJwt() {
  return removeCookie(KeyJwt)
}

// ========== 处理用户名的cookie

export function getCookieUsername() {
  return getCK(KeyUsername)
}

export function setCookieUsername(username) {
  return setCK(KeyUsername, username)
}

export function removeCookieUsername() {
  return removeCookie(KeyUsername)
}

// ========== 处理管理员密码的cookie

export function getCookiePassword() {
  return getCK(KeyPassword)
}

export function setCookiePassword(password) {
  return setCK(KeyPassword, password)
}

export function removeCookiePassword() {
  return removeCookie(KeyPassword)
}

// ========== 处理上传配置的cookie

export function getCookieConfigUpload() {
  return getCKObj(KeyConfigUpload)
}

export function setCookieConfigUpload(configUpload) {
  return setCKObj(KeyConfigUpload, configUpload)
}

export function removeCookieConfigUpload() {
  return removeCookie(KeyConfigUpload)
}

// ========== 处理系统配置的cookie

export function getCookieConfigSystem() {
  return getCKObj(KeyConfigSystem)
}

export function setCookieConfigSystem(configSystem) {
  return setCKObj(KeyConfigSystem, configSystem)
}

export function removeCookieConfigSystem() {
  return removeCookie(KeyConfigSystem)
}
